package com.wuliu.driver.controller;

import com.wuliu.driver.pojo.Freightsources;
import com.wuliu.driver.pojo.Route;
import com.wuliu.driver.service.FreightsourcesService;
import com.wuliu.driver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//货源信息表
@RestController
@RequestMapping("/freightsources")
public class FreightsourcesController {
    @Autowired
    private FreightsourcesService ff;
    @RequestMapping("/guazai")
    public Result guazai(){
        System.out.println("获取货源信息");
        List<Freightsources> huoyuan = ff.selectAll();
        System.out.println(huoyuan);
        System.out.println(huoyuan.get(0));
        return new Result(20041,huoyuan,"获取货源信息成功");
    }

    //搜索货源
    @GetMapping("/sousuoaa/{name}")
    public Result search(@PathVariable String  name){
        System.out.println("搜索货源");
        List<Map> selectss = ff.selectss(name);
        if (selectss!=null){
            return new Result(20041,selectss,"搜索货源成功");
        }else {
            return new Result(20020,"搜索货源失败");
        }
    }
    @GetMapping("/cangpao/{id}")
    public Result changpao(@PathVariable Integer id){
        System.out.println("获取常跑路线");
        List<Route> routes = ff.selectRoute(id);
        return new Result(20041,routes,"成功获取到常跑路线");
    }

    @GetMapping("/del/{id}")
    public Result del(@PathVariable Integer id){
        System.out.println("删除常跑路线");
        int i = ff.deleroute(id);
        if (i>0){
            return new Result(20021,"删除成功");
        }else {
            return new Result(20020,"删除失败");
        }
    }

    //专属货源挂载查询
    @GetMapping("/zhuanshu/{id}")
    public Result zhuanshu(@PathVariable Integer id){
        System.out.println("专属货源挂载查询");
        List<Freightsources> zhuanshu = ff.selezhuanshu(id);
        return new Result(20041,zhuanshu,"成功获取专属货源");
    }

    //新增常跑路线
    @RequestMapping("/cpao")
    public Result  cpao(@RequestBody Route route){
        System.out.println("新增常跑路线");
        List<Route> routes = ff.selectRoute(route.getDid());
        if (routes.size()>=10){
            return new Result(20020,"常跑路线已达上限");
        }else {
            int i = ff.inchang(route);
            if (i>0){
                return new Result(20021,"新增成功");
            }else {
                return new Result(20020,"新增失败");
            }
        }

    }
}
